﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/14
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Components.MikeLoggor
{
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using MikeWare.Components.MikeLoggor.Listeners;

    public abstract class Listener
    {
        public Settings Setting { get; set; }
        public Loggor CurrentLoggor { get; private set; }
        public virtual void InitAttributes(IList<Settings> attributes) { throw new NotImplementedException(); }
        public virtual void Write(DateTime logTime, String message) { throw new NotImplementedException(); }
        public virtual void WriteLine() { throw new NotImplementedException(); }
        public virtual void Flush() { throw new NotImplementedException(); }
        public virtual void Rolling() { throw new NotImplementedException(); }
        public virtual void Close() { throw new NotImplementedException(); }

        public Listener(Loggor loggor, Settings setting)
        {
            CurrentLoggor = loggor;
            Setting = setting;
        }

        internal static Listener BuildListener(Loggor loggor, Settings setting)
        {
            if (null == setting) return null;

            switch (setting.Name.Trim().ToUpper())
            {
                case "FILEWRITERLISTENER":
                    var filewriterlistener = new FileWriterListener(loggor, setting);
                    return filewriterlistener;
                case "TRACEWRITERLISTENER":
                    var traceWriterListener = new TraceWriterListener(loggor, setting);
                    return traceWriterListener;
                default:
                    return null;
            }
        }

        internal static IList<Listener> BuildListeners(Loggor loggor, IList<Settings> settings)
        {
            var result = new List<Listener>();

            foreach (var setting in settings)
            {
                var listener = BuildListener(loggor, setting);
                if (null != listener) result.Add(listener);
            }

            return result;
        }

        internal void ReplaceDateTimeAttributes(ref string value, DateTime logFileTime)
        {
            var attributePattern = @"%d\{(?<time>[^}]+?)}";
            Regex regex = new Regex(attributePattern);
            var matches = regex.Matches(value);
            if (null == matches || 0 == matches.Count) return;

            foreach (Match match in matches)
            {
                var time = match.Groups["time"].Value;
                var matchPattern = @"%d\{" + time + @"}";
                value = Regex.Replace(value, matchPattern, logFileTime.ToString(time));
            }
        }

        internal void ReplaceReferenceAttributes(ref string value)
        {
            if (null == CurrentLoggor.GlobalSettings || 0 == CurrentLoggor.GlobalSettings.Count) return;

            var attributePattern = @"\$\{(?<name>[^}]+?)}";
            Regex regex = new Regex(attributePattern);
            var matches = regex.Matches(value);
            if (null == matches || 0 == matches.Count) return;

            foreach (Match match in matches)
            {
                var name = match.Groups["name"].Value;
                var matchPattern = @"\$\{" + name + @"}";
                string settingValue = null;
                foreach (var setting in CurrentLoggor.GlobalSettings)
                {
                    if (setting.FoundValueByName(name, out settingValue))
                    {
                        value = Regex.Replace(value, matchPattern, settingValue);
                        break;
                    }
                }
            }
        }
    }
}
